package com.flyqiu.flow.api.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyqiu.common.tools.StringTools;
import com.flyqiu.common.user.FlyQiuUser;
import com.flyqiu.flow.model.entity.hi.ProcHiIdentityLink;
import com.flyqiu.flow.model.entity.ru.ProcRuIdentityLink;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public interface IdentityLinkService {
    default void add(ProcRuIdentityLink link) {
        ArrayList<ProcRuIdentityLink> list = new ArrayList<>();
        list.add(link);
        add(list);
    }

    void actAdd(String procInstId, String actId, String actInstId, String type, Collection<? extends FlyQiuUser> users);

    void add(Collection<ProcRuIdentityLink> links);

    void moveToHistory(String procInstId, String actId);

    default void moveToHistory(String procInstId) {
        moveToHistory(procInstId, null);
    }

    default List<ProcRuIdentityLink> getRuIdentityLinksByActId(String actId) {
        LambdaQueryWrapper<ProcRuIdentityLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcRuIdentityLink::getActId, actId);
        return getRuIdentityLinks(queryWrapper);
    }

    default List<ProcRuIdentityLink> getRuIdentityLinksByProcInstId(String procInstId) {
        LambdaQueryWrapper<ProcRuIdentityLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcRuIdentityLink::getProcInstId, procInstId);
        return getRuIdentityLinks(queryWrapper);
    }

    default void deleteRuIdentityLink(String procInstId) {
        LambdaQueryWrapper<ProcRuIdentityLink> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProcRuIdentityLink::getProcInstId, procInstId);
        deleteRuIdentityLink(lambdaQueryWrapper);
    }

    default void deleteRuIdentityLink(String procInstId, String actId) {
        LambdaQueryWrapper<ProcRuIdentityLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcRuIdentityLink::getProcInstId, procInstId);
        queryWrapper.eq(StringTools.isNotBlank(actId), ProcRuIdentityLink::getActId, actId);
        deleteRuIdentityLink(queryWrapper);
    }


    default void deleteRuIdentityLink(String actId, FlyQiuUser flowUser) {
        LambdaQueryWrapper<ProcRuIdentityLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcRuIdentityLink::getActId, actId);
        queryWrapper.eq(ProcRuIdentityLink::getUserId, flowUser.getUniqueKey());

        deleteRuIdentityLink(queryWrapper);
    }

    void deleteRuIdentityLink(LambdaQueryWrapper<ProcRuIdentityLink> queryWrapper);


    default List<ProcRuIdentityLink> listRuIdentityLinks(String procInstId, String actId, String actInstId, String type) {
        LambdaQueryWrapper<ProcRuIdentityLink> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProcRuIdentityLink::getProcInstId, procInstId);
        lambdaQueryWrapper.eq(ProcRuIdentityLink::getActId, actId);
        lambdaQueryWrapper.eq(ProcRuIdentityLink::getActInstId, actInstId);
        lambdaQueryWrapper.eq(ProcRuIdentityLink::getType, type);
        return getRuIdentityLinks(lambdaQueryWrapper);
    }

    List<ProcRuIdentityLink> getRuIdentityLinks(LambdaQueryWrapper<ProcRuIdentityLink> queryWrapper);

    default List<ProcHiIdentityLink> getHiIdentityLinksByActId(String actId) {
        LambdaQueryWrapper<ProcHiIdentityLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcHiIdentityLink::getActId, actId);
        return getHiIdentityLinks(queryWrapper);
    }

    default List<ProcHiIdentityLink> getHiIdentityLinksByProcInstId(String procInstId) {
        LambdaQueryWrapper<ProcHiIdentityLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcHiIdentityLink::getProcInstId, procInstId);
        return getHiIdentityLinks(queryWrapper);
    }

    List<ProcHiIdentityLink> getHiIdentityLinks(LambdaQueryWrapper<ProcHiIdentityLink> queryWrapper);

    default void deleteHiIdentityLink(String procInstId) {
        deleteHiIdentityLink(procInstId, null);
    }

    default void deleteHiIdentityLink(String procInstId, String actId) {
        LambdaQueryWrapper<ProcHiIdentityLink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcHiIdentityLink::getProcInstId, procInstId);
        queryWrapper.eq(StringTools.isNotBlank(actId), ProcHiIdentityLink::getActId, actId);
        deleteHiIdentityLink(queryWrapper);
    }


    void deleteHiIdentityLink(LambdaQueryWrapper<ProcHiIdentityLink> queryWrapper);


}
